/**
Nectarine Utility Project is a software interface for the web system 'demovibes'
Copyright (C) 2010  Alexandre LAURENT

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

website: http://code.google.com/p/nectarine-utility-project/
e-mail: lw.demoscene@gmail.com
**/

#include "streamparser.h"

#include <QXmlStreamReader>
#include <QList>
#include <QByteArray>
#include <QString>

#include <QDebug>

#include "../Types/stream.h"
#include "../utils.h"

bool readStreamEntry(QXmlStreamReader& xmlReader, Stream& s)
{
    bool isOk = true;
    QXmlStreamReader::TokenType nodeType = xmlReader.tokenType();

    while ( (nodeType != QXmlStreamReader::EndElement || xmlReader.name() != "stream") && isOk )
    {
        if ( nodeType == QXmlStreamReader::StartElement )
        {
            if ( xmlReader.name() == "stream" )
            {
                s.id = xmlReader.attributes().value("id").toString().toUInt();
            }
            else if ( xmlReader.name() == "url" )
            {
                QString urlString;
                isOk &= Utils::getXMLStringData(xmlReader,urlString);
                s.url = QUrl(urlString);
            }
            else if ( xmlReader.name() == "name" )
            {
                isOk &= Utils::getXMLStringData(xmlReader,s.name);
            }
            else if ( xmlReader.name() == "country" )
            {
                isOk &= Utils::getXMLStringData(xmlReader,s.flag);
            }
            else if ( xmlReader.name() == "bitrate" )
            {
                QString bitrateString;
                isOk &= Utils::getXMLStringData(xmlReader,bitrateString);
                if ( isOk )
                {
                    s.bitrate = bitrateString.toUInt(&isOk);
                }
            }
            else if ( xmlReader.name() == "type" )
            {
                isOk &= Utils::getXMLStringData(xmlReader,s.type);
            }
            else
            {
                qDebug() << "One tag is not parsed at all -> " << xmlReader.name();
            }
        }

        nodeType = xmlReader.readNext();
    }

    return isOk;
}

bool Parser :: readXMLStream(const QByteArray& streamData, QList<Stream>& streams)
{
    QXmlStreamReader xmlReader(streamData);
    QXmlStreamReader::TokenType nodeType;
    bool isOk = true;

    qDebug() << "Parser :: readXMLStream";

    while ( !xmlReader.atEnd() )
    {
        nodeType = xmlReader.readNext();

        if ( nodeType == QXmlStreamReader::StartElement && xmlReader.name() == "streams" )
        {
            while(nodeType != QXmlStreamReader::EndElement || xmlReader.name() != "streams")
            {
                if ( nodeType == QXmlStreamReader::StartElement && xmlReader.name() == "stream" )
                {
                    Stream s;
                    isOk &= readStreamEntry(xmlReader, s);
                    streams.push_back(s);
                }

                nodeType = xmlReader.readNext();
            }
        }
    }

    qDebug() << streams;

    return isOk;
}
